package com.jaychen.tliaswebmanagement.controller;

import com.jaychen.tliaswebmanagement.pojo.ClassQueryParam;
import com.jaychen.tliaswebmanagement.pojo.Clazz;
import com.jaychen.tliaswebmanagement.pojo.PageResult;
import com.jaychen.tliaswebmanagement.pojo.Result;
import com.jaychen.tliaswebmanagement.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result page(ClassQueryParam classQueryParam){
        log.info("班级列表查询: {}", classQueryParam);
        PageResult<Clazz> pageResult = clazzService.page(classQueryParam);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据ID删除班级: {}",id);
        clazzService.delete(id);
        return Result.success();
    }

    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        log.info("添加班级: {}",clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询班级信息: {}",id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("更新班级: {}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        log.info("查询所有班级");
        List<Clazz> clazzList = clazzService.getAll();
        return Result.success(clazzList);
    }
}
